import 'package:extended_image/extended_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:xws/models/user/user_update_obj.dart';
import 'package:xws/state/user_state.dart';
import 'package:xws/utils/app_util.dart';
import 'package:xws/utils/dio_utils/qiniu_oss.dart';
import 'package:xws/utils/dio_utils/request/user_req.dart';
import 'package:xws/utils/tools_util.dart';
import 'package:xws/widgets/crop_image_page.dart';

/// 编辑头像
Future<void> editHeader(BuildContext context) async {
  final String? img = (await pickImgs(context))?.single;
  if (img == null) return;

  final File? f = await push<File>(
    context,
    CropImgPage(
      img: File(img),
      fileName: 'header_${us.info.id}_${DateTime.now().millisecondsSinceEpoch}',
    ),
  );
  if (f == null) return;

  loading(context);
  final List<String> rImg = await QiniuOSS.uploadFiles(
    <String>[f.path],
    Dir.head,
  );

  if (rImg.isEmpty) {
    close(context);
    return;
  }

  final bool r = await updateInfo(UserUpdateObj(avatarUrl: rImg.single));

  close(context);

  if (r) await us.updateBasicInfo();
}
